package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.acticle.service.IStrategyDetailService;
import cn.wolfcode.luowowo.acticle.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.prefix.StrategyStatisKeyPrefix;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 容器关闭后初始化数据
 * redis数据落地监听器
 */
@Component
public class RedisDataDestoryListener implements ApplicationListener<ContextClosedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        System.out.println("------------begin-------------------");
        String keyPattern = StrategyStatisKeyPrefix.STRATEGY_STATIS_VO.getPrefix() + ":*";
        List<StrategyStatisVO> list = strategyStatisRedisService.getStatisVOs(keyPattern);
        for (StrategyStatisVO vo : list) {
            strategyDetailService.updateStatis(vo);
        }
        System.out.println("------------end-------------------");
    }
}